var GetQueryString = function (name) {
	var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
	var r = window.location.search.substr(1).match(reg);
	return r ? r[2] : null;
};
var source = GetQueryString('source') || '';
var urlModels = {
	'hhgdt': {
		'css': [
			'//hh.ppdaicdn.com/static/styles/register/index.css?v=2',
			'//hh.ppdaicdn.com/static/styles/activities/guangdiantong/index.css?v=1'
		],
		'img': {
			'img_bkg_01': '//hh.ppdaicdn.com/static/images/activities/guangdiantong/bkg_01_v2.png?v=1',
			'img_bkg_02': '//m.ppdaicdn.com/act/huan/img/register/bkg_02.png?v=2',
			'img_bkg_03': '//hh.ppdaicdn.com/static/images/activities/guangdiantong/bkg_03.png?v=1',
			'img_bkg_04': '//hh.ppdaicdn.com/static/images/activities/guangdiantong/bkg_04.png?v=1',
			'img_bkg_05': GetQueryString('tophost') !== 'ppdai' ? '//m.ppdaicdn.com/act/huan/img/register/bkg_05_hkcr.png?v=1' : '//m.ppdaicdn.com/act/huan/img/register/bkg_05.png?v=2',
			'img_icon_refresh': '//m.ppdaicdn.com/act/huan/img/register/icon_refresh.png?v=2',
			'img_download_alert': '//m.ppdaicdn.com/act/huan/img/register/download_alert.png?v=2'
		},
		'bkg': {
			'register-form-submit': '//hh.ppdaicdn.com/static/images/activities/guangdiantong/btn.png?v=1'
		}
	},
	'qmm': {
		'css': [
			'//hh.ppdaicdn.com/static/styles/activities/index.css?v=7'
		],
		'img': {
			'img_bkg_01': '//hh.ppdaicdn.com/static/images/activities/quanmama/bkg_v4_01.png?v=1',
			'img_bkg_02': '',
			'img_bkg_03': '//hh.ppdaicdn.com/static/images/activities/quanmama/bkg_v4_03.png?v=1',
			'img_bkg_04': '//hh.ppdaicdn.com/static/images/activities/quanmama/bkg_v4_04.png?v=1',
			'img_bkg_05': '//hh.ppdaicdn.com/static/images/activities/quanmama/bkg_v4_05.png?v=1',
			'img_icon_refresh': '//m.ppdaicdn.com/act/huan/img/register/icon_refresh.png?v=2',
			'img_download_alert': '//m.ppdaicdn.com/act/huan/img/register/download_alert.png?v=2'
		},
		'bkg': {
			'register-form-submit': '//hh.ppdaicdn.com/static/images/activities/quanmama/btn_v3.png?v=1',
			'app-download': '//hh.ppdaicdn.com/static/images/activities/quanmama/btn_alert_v3.png?v=1'
		}
	},
	'woaika': {
		'css': [
			'//hh.ppdaicdn.com/static/styles/activities/woaika/index.css?v=2'
		],
		'img': {
			'img_bkg_01': '//hh.ppdaicdn.com/static/images/activities/woaika/bkg_v3_01.png?v=1',
			'img_bkg_02': '//hh.ppdaicdn.com/static/images/activities/woaika/bkg_v3_02.png?v=1',
			'img_bkg_03': '//hh.ppdaicdn.com/static/images/activities/woaika/bkg_v3_03.png?v=1',
			'img_bkg_04': '//hh.ppdaicdn.com/static/images/activities/woaika/bkg_v3_04.png?v=1',
			'img_bkg_05': '//hh.ppdaicdn.com/static/images/activities/woaika/bkg_v3_05.png?v=1',
			'img_icon_refresh': '//hh.ppdaicdn.com/static/images/activities/woaika/icon_refresh_v3.png?v=1',
			'img_download_alert': '//hh.ppdaicdn.com/static/images/register/download_alert_v3.png?v=2'
		},
		'bkg': {
			'register-form-submit': '//hh.ppdaicdn.com/static/images/activities/woaika/btn_v3.png?v=1'
		}
	},
	/*'*': {
		'css': [
			'//m.ppdaicdn.com/act/huan/css/register/index_v4.css?v=2'
		],
		'img': {
			'img_bkg_01': '//m.ppdaicdn.com/act/huan/img/register/bkg_v4_01.png?v=1',
			'img_bkg_02': '//m.ppdaicdn.com/act/huan/img/register/bkg_v4_02.png?v=1',
			'img_bkg_03': '//m.ppdaicdn.com/act/huan/img/register/bkg_v4_03.png?v=1',
			'img_bkg_04': '//m.ppdaicdn.com/act/huan/img/register/bkg_v4_04.png?v=1',
			'img_bkg_05': '//m.ppdaicdn.com/act/huan/img/register/bkg_v4_05.png?v=1',
			'img_icon_refresh': '//m.ppdaicdn.com/act/huan/img/register/icon_refresh_v4.png?v=1',
			'img_download_alert': '//m.ppdaicdn.com/act/huan/img/register/download_alert_v4.png?v=1'
		},
		'bkg': {
			'register-form-submit': '//m.ppdaicdn.com/act/huan/img/register/btn_v4.png?v=5',
			//'app-download': '//m.ppdaicdn.com/act/huan/img/register/btn_alert_v4.png?v=1'
		}
	}*/
	'*': {
		'css': [
			'//hh.ppdaicdn.com/static/styles/register/index_v3.css?v=1'
		],
		'img': {
			'img_bkg_01': '//hh.ppdaicdn.com/static/images/register/bkg_v5_01.png?v=1',
			'img_bkg_02': '',
			'img_bkg_03': '//hh.ppdaicdn.com/static/images/register/bkg_v3_03.png?v=2',
			'img_bkg_04': '//hh.ppdaicdn.com/static/images/register/bkg_v3_04.png?v=2',
			'img_bkg_05': GetQueryString('tophost') !== 'ppdai' ? '//m.ppdaicdn.com/act/huan/img/register/bkg_05_hkcr.png?v=1' : '//hh.ppdaicdn.com/static/images/register/bkg_v3_05.png?v=2',
			'img_icon_refresh': '//hh.ppdaicdn.com/static/images/register/icon_refresh_v3.png?v=2',
			'img_download_alert': '//hh.ppdaicdn.com/static/images/register/download_alert_v3.png?v=2'
		},
		'bkg': {
			'register-form-submit': '//hh.ppdaicdn.com/static/images/register/btn_v3.png?v=2'
		}
	}
};
var urls = {
	'hhgdt1': urlModels.hhgdt,
	'hhgdt2': urlModels.hhgdt,
	'hhgdt3': urlModels.hhgdt,
	'hhgdt4': urlModels.hhgdt,
	'hhgdt5': urlModels.hhgdt,
	'hhgdt6': urlModels.hhgdt,
	'hhgdt7': urlModels.hhgdt,
	'hhgdt8': urlModels.hhgdt,
	'hhgdt9': urlModels.hhgdt,
	'hhgdt10': urlModels.hhgdt,
	'hhgdtb1': urlModels.hhgdt,
	'hhgdtb2': urlModels.hhgdt,
	'hhgdtb3': urlModels.hhgdt,
	'hhgdtb4': urlModels.hhgdt,
	'hhgdtb5': urlModels.hhgdt,
	'hhgdtb6': urlModels.hhgdt,
	'hhgdtb7': urlModels.hhgdt,
	'hhgdtb8': urlModels.hhgdt,
	'hhgdtb9': urlModels.hhgdt,
	'hhgdtb10': urlModels.hhgdt,
	'hhgdtb11': urlModels.hhgdt,
	'hhgdtb12': urlModels.hhgdt,
	'hhgdtb13': urlModels.hhgdt,
	'hhgdtb14': urlModels.hhgdt,
	'hhgdtb15': urlModels.hhgdt,
	'hhgdtb16': urlModels.hhgdt,
	'hhgdtb17': urlModels.hhgdt,
	'hhgdtb18': urlModels.hhgdt,
	'hhgdtb19': urlModels.hhgdt,
	'hhgdtb20': urlModels.hhgdt,
	'qmm': urlModels.qmm,
	'hhwoaika1': urlModels.woaika,
	'hhwoaika2': urlModels.woaika,
	'hhwoaika3': urlModels.woaika,
	'hhwoaika4': urlModels.woaika,
	'hhwoaika5': urlModels.woaika,
	'*': urlModels['*']
};
var isActivity = !!urls[source];
var urlsObj = isActivity ? urls[source] : urls['*'];
for (var i = 0; i < urlsObj.css.length; ++i) {
	$('<link>').attr({
		'href': urlsObj.css[i],
		'rel': 'stylesheet'
	}).appendTo('head');
}
for (var i in urlsObj.img) {
	$('#' + i + '').attr('src', urlsObj.img[i]);
}
for (var i in urlsObj.bkg) {
	$('#' + i + '').css('background-image', 'url(' + urlsObj.bkg[i] + ')');
}
// 加载js
var loadScript = function (url) {
	var $dynamicScript = document.createElement('script');
	$dynamicScript.type = 'text/javascript';
	$dynamicScript.async = true;
	$dynamicScript.src = url;
	document.body.appendChild($dynamicScript);
};
// 加载统计
GetQueryString('tophost') === 'ppdai' && loadScript('//m.ppdaicdn.com/act/huan/js/sensors_analytics.js?v=3');

//今日头条
var jrttObj = {
	'jrttxxl': {
		'convert_id': '67522217269',
		'event_type': 'form'
	},
	'jrttxxl1': {
		'convert_id': '67522217269',
		'event_type': 'form'
	},
	'jrttxxl2': {
		'convert_id': '67522217269',
		'event_type': 'form'
	},
	'jrttxxl3': {
		'convert_id': '67522217269',
		'event_type': 'form'
	},
	'jrttxxl4': {
		'convert_id': '67522217269',
		'event_type': 'form'
	},
	'jrttxxl5': {
		'convert_id': '67522217269',
		'event_type': 'form'
	},
	'jrttxxl6': {
		'convert_id': '67522217269',
		'event_type': 'form'
	},
	'jrttxxl7': {
		'convert_id': '67522217269',
		'event_type': 'form'
	},
	'jrttxxl8': {
		'convert_id': '67522217269',
		'event_type': 'form'
	},
	'jrttxxl9': {
		'convert_id': '67522217269',
		'event_type': 'form'
	},
	'jrttxxl10': {
		'convert_id': '67522217269',
		'event_type': 'form'
	},
	'jrttxxlB1': {
		'convert_id': '67532904348',
		'event_type': 'form'
	},
	'jrttxxlB2': {
		'convert_id': '67532904348',
		'event_type': 'form'
	},
	'jrttxxlB3': {
		'convert_id': '67532904348',
		'event_type': 'form'
	},
	'jrttxxlB4': {
		'convert_id': '67532904348',
		'event_type': 'form'
	},
	'jrttxxlB5': {
		'convert_id': '67532904348',
		'event_type': 'form'
	},
	'jrttxxlB6': {
		'convert_id': '67532904348',
		'event_type': 'form'
	},
	'jrttxxlB7': {
		'convert_id': '67532904348',
		'event_type': 'form'
	},
	'jrttxxlB8': {
		'convert_id': '67532904348',
		'event_type': 'form'
	},
	'jrttxxlB9': {
		'convert_id': '67532904348',
		'event_type': 'form'
	},
	'jrttxxlB10': {
		'convert_id': '67532904348',
		'event_type': 'form'
	}
};
(function(root) {
	if (source in jrttObj) {
		root._tt_config = true;
		var ta = document.createElement('script');
		ta.type = 'text/javascript';
		ta.async = true;
		ta.src = document.location.protocol + '//' + 's3.pstatp.com/bytecom/resource/track_log/src/toutiao-track-log.js';
		ta.onerror = function () {
			var request = new XMLHttpRequest();
			var web_url = window.encodeURIComponent(window.location.href);
			var js_url  = ta.src;
			var url = '//ad.toutiao.com/link_monitor/cdn_failed?web_url=' + web_url + '&js_url=' + js_url + '&convert_id=' + jrttObj[source].convert_id;
			request.open('GET', url, true);
			request.send(null);
		};
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ta, s);
	}
})(window);

$(function () {
	var uaClient = function () {
		var ua = navigator.userAgent.toLowerCase();
		if (ua.match(/MicroMessenger/i) == 'micromessenger') {
			return 'wechat';
		} else if (ua.match(/iPad/i) || ua.match(/iPhone/i) || ua.match(/iPod/i)) {
			return 'ios';
		} else {
			return 'android';
		}
	};
	if (uaClient() === 'ios' || uaClient() === 'wechat') {
		$('.register').css({
			'-webkit-overflow-scrolling': 'touch'
		}).attr('style', '-webkit-overflow-scrolling: touch;');
	}
	if (uaClient() === 'android') {
		$('.register').css({
			'position': 'static',
			'overflow-y': 'auto',
			'height': 'auto'
		}).attr('style', 'position: static; overflow-y: auto; height: auto;');
	}
	/*TO REMOVE START*/
	/*$('#download-alert.show').css({
		'display': 'flex',
		'justify-content': 'center',
		'align-items': 'center'
	});
	$('#download-alert > div').css({
		'position': 'relative'
	});*/
	$('.register-form input').css({
		'margin': '0 -1em',
		'padding': '0 1em',
		'-webkit-tap-highlight-color': 'transparent',
		'-webkit-touch-callout': 'none'
	});
	$('#sms-code-button').parent().css({
		'-webkit-user-select': 'none',
		'user-select': 'none'
	});
	/*TO REMOVE END*/
	var valuePhone = '',
		valueImgCode = '',
		valueSMSCode = '',
		valuePassword = '';
	var currValid = 0;
	var needValidPhone = true,
		needValidImgCode = true,
		needValidSMSCode = true,
		needValidPassword = true;
	var sendTimeout = null,
		errorAlertTimeout = null;
	var ignoreValidImgCode = 0;
	var newId = '';
	var isNewUserAjaxAbort = false;
	var inputLocating = false;
	var currTop = 0;
	var registeredOrNew = 0;
	var validPhone = function (silent) {
		if (!needValidPhone) {
			return;
		}
		var phone = $.trim($('#phone-input').val());
		if (phone.length == 0) {
			silent || errorShow('请先输入手机号');
			currValid = 1;
			return false;
		}
		if (!/^1[34578]\d{9}$/.test(phone)) {
			silent || errorShow('手机号格式错误，请重新输入');
			return false;
		}
		return true;
	};
	var validImgCode = function (silent) {
		if (!needValidImgCode) {
			return;
		}
		var imgCode = $.trim($('#image-code-input').val());
		if (imgCode.length == 0) {
			silent || errorShow('请先输入图形验证码');
			currValid = 2;
			return false;
		}
		return true;
	};
	var validSMSCode = function (silent) {
		if (!needValidSMSCode) {
			return;
		}
		var smsCode = $.trim($('#sms-code-input').val());
		if (smsCode.length == 0) {
			silent || errorShow('请先输入短信验证码');
			currValid = 3;
			return false;
		}
		if (ignoreValidImgCode === 1) {
			ignoreValidImgCode === 2;
		}
		return true;
	};
	var validPassword = function (silent) {
		if (!needValidPassword) {
			return;
		}
		var password = $.trim($('#password-input').val());
		if (password.length == 0) {
			silent || errorShow('请先设置密码');
			currValid = 4;
			return false;
		}
		if (!/^[0-9A-Za-z]{6,16}$/.test(password) || !/[A-Za-z]/.test(password) || !/[0-9]/.test(password)) {
			silent || errorShow('密码格式错误，请重新输入');
			return false;
		}
		return true;
	};
	var sendSMSCode = function () {
		return $.ajax({
			'type': 'get',
			'url': '//ac.ppdai.com/ValidateCode/MobileWithImgCode',
			'data': {
				'mobilePhone': valuePhone,
				'imgCode': valueImgCode
			},
			'async': false
		});
	};
	var setResendStatus = function (sec) {
		$('#sms-code-button').next().html(sec + 's');
		if (sec == 0) {
			clearTimeout(sendTimeout);
			$('#sms-code-button').next().html('60s').parent().removeClass('sending');
			return;
		}
		sendTimeout = setTimeout(function () {
			setResendStatus(sec - 1);
		}, 1000);
	};
	var checkImgCode = function () {
		return $.ajax({
			'type': 'get',
			'url': '//ac.ppdai.com/registercheck',
			'data': {
				'name': 'imgvalidatecode',
				'value': valueImgCode
			},
			'async': false
		});
	};
	var checkSMSCode = function () {
		return $.ajax({
			'type': 'get',
			'url': '//ac.ppdai.com/registercheck',
			'data': {
				'name': 'mobilevalidatecode',
				'value': valueSMSCode + ',' + valuePhone
			},
			'async': false
		});
	};
	var errorShow = function (msg) {
		errorHide();
		errorAlertTimeout = setTimeout(function () {
			$('#error-alert').find('#error-message').html(msg)
				.end().addClass('show');
			if (uaClient() === 'android') {
				$('#error-alert').css({
					'display': 'flex',
					'justify-content': 'center',
					'align-items': 'center'
				}).children('div').css({
					'position': 'relative',
					'top': '0',
					'left': '0',
					'margin-top': '0',
					'margin-left': '0'
				});
			}
			errorAlertTimeout = setTimeout(errorHide, 2000);
		}, 0);
	};
	var errorHide = function () {
		$('#error-alert').find('#error-message').html('')
			.end().removeClass('show');
		if (uaClient() === 'android') {
			$('#error-alert').css({
				'display': 'none'
			});
		}
		clearTimeout(errorAlertTimeout);
	};
	var showRegisteredUserAlert = function () {
		registeredOrNew = 0;
		// 统计
		sa.track('h5_element_imp', {
			'tgt_type': 'BOX',
			'tgt_name': '还还-老用户弹框',
			'tgt_event_id': 'hh_oldCustomers_box',
			'url': location.href,
			'position': ''
		});
		if (isActivity) {
			$('#download-alert').find('h2').html('该手机号已注册！')
				.end().find('p').html('还卡超人是拍拍贷旗下产品 拍拍貸账号可直接下载App 领取额度哦～')
				.end().addClass('show');
		} else {
			$('#download-alert').find('h2').html('拍拍贷老用户直接下载')
				.end().find('p').html('还卡超人是拍拍贷旗下信用卡代偿产品，您可以直接使用拍拍贷账号登录还卡超人App领取额度哦～')
				.end().addClass('show');
		}
	};
	var showNewUserAlert = function () {
		registeredOrNew = 1;
		// 统计
		sa.track('h5_element_imp', {
			'tgt_type': 'BOX',
			'tgt_name': '还还-新用户弹框',
			'tgt_event_id': 'hh_newCustomers_box',
			'url': location.href,
			'position': ''
		});
		$('#download-alert').find('h2').html('下载还卡超人App')
			.end().find('p').html('恭喜您注册成功！')
			.end().addClass('show');
	};
	var inputLocate = function () {
		if (uaClient() === 'ios') {
			return;
		}
		inputLocating = true;
		$('#register-form').closest('.block').get(0).scrollIntoView();
		//$('.register').scrollTop($('.head').first().height() - 16);
	};
	var cookieResultArr = null;
	var localTimestamp = new Date().getTime();

	$('.register').on('touchmove', function () {
		if (inputLocating) {
			inputLocating = false;
			return;
		}
		if (currTop === $('.register').scrollTop()) {
			return;
		}
		currTop = $('.register').scrollTop();
		$('#phone-input, #image-code-input, #sms-code-input, #password-input').trigger('blur', true);
	});
	$('#phone-input').on('focus', function () {
		currValid > 1 && errorHide();
		currValid = 0;
		inputLocate();
	}).on('input', function () {
		if ($('#phone-input').val() === '') {
			$('#phone-clear').hide();
		} else {
			$('#phone-clear').show();
		}
		if (!validPhone(true)) {
			return;
		}
		valuePhone = $.trim($('#phone-input').val());
	}).on('blur', function (evt, justBlur) {
		// 统计
		sa.track('h5_input', {
			'tgt_type': 'text',
			'tgt_name': '还还-注册页-手机号',
			'tgt_event_id': 'hh_register_phoneNumber',
			'url': location.href,
			'position': ''
		});
		if (justBlur) {
			return;
		}
		if ($('#phone-input').val() === '') {
			$('#phone-clear').hide();
		} else {
			$('#phone-clear').show();
		}
		if (!validPhone(true)) {
			return;
		}
		valuePhone = $.trim($('#phone-input').val());
		valueSMSCode = '';
		valuePassword = '';
		$.ajax({
			'type': 'get',
			'async': false,
			'dataType': "jsonp",
			'url': "//m.ppdai.com/Activity/ThirdInviteNewUserRecord2",
			'data': {
				'mobile': valuePhone,
				'source': source,
				'subsource': JSON.stringify({
					'subsource': 'ONE_STEP'
				})
			}
		}).done(function (data) {
			if (isNewUserAjaxAbort) {
				isNewUserAjaxAbort = false;
				return;
			}
			if (data.ResultMessage) {
				if (data.Content.IsNewUser) {
					newId = data.Content.Id;
				} else {
					needValidPhone = false;
					needValidImgCode = false;
					needValidSMSCode = false;
					needValidPassword = false;
					setTimeout(function () {
						$('#image-code-input, #sms-code-input, #password-input').trigger('blur');
						if (uaClient() === 'android') {
							$('#download-alert').css({
								'display': 'flex',
								'justify-content': 'center',
								'align-items': 'center'
							}).children('div').css({
								'position': 'relative',
								'top': '0',
								'left': '0',
								'margin-top': '0',
								'margin-left': '0'
							});
						}
						needValidPhone = true;
						needValidImgCode = true;
						needValidSMSCode = true;
						needValidPassword = true;
					}, 0);
					showRegisteredUserAlert();
					$('.register').on('touchmove scroll', function () {
						return false;
					});
				}
			}
		}).fail(function(XMLHttpRequest, textStatus, errorThrown) {
			if (isNewUserAjaxAbort) {
				isNewUserAjaxAbort = false;
				return;
			}
			needValidPhone = false;
			needValidImgCode = false;
			needValidSMSCode = false;
			needValidPassword = false;
			$('#image-code-input, #sms-code-input, #password-input').trigger('blur');
			alert("网络错误");
			needValidPhone = true;
			needValidImgCode = true;
			needValidSMSCode = true;
			needValidPassword = true;
		});
	});
	$('#image-code-input').on('focus', function () {
		currValid > 2 && errorHide();
		currValid = 0;
		inputLocate();
	}).on('input change', function () {
		if (ignoreValidImgCode !== 2 && !validImgCode(true)) {
			return;
		}
		valueImgCode = $.trim($('#image-code-input').val());
	}).on('blur', function (evt, justBlur) {
		// 统计
		sa.track('h5_input', {
			'tgt_type': 'text',
			'tgt_name': '还还-注册页-图形验证码',
			'tgt_event_id': 'hh_register_graphicCode',
			'url': location.href,
			'position': ''
		});
		if (justBlur) {
			return;
		}
		if (ignoreValidImgCode !== 2 && !validImgCode(true)) {
			return;
		}
		valueImgCode = $.trim($('#image-code-input').val());
		checkImgCode().done(function (res) {
			if (res.Code != 1) {
				//silent
				//errorShow('图形验证码输入错误，请重新输入');
				return;
			}
		});
	});
	$('#sms-code-input').on('focus', function () {
		currValid > 3 && errorHide();
		currValid = 0;
		inputLocate();
	}).on('input blur', function () {
		if (!validSMSCode(true)) {
			return;
		}
		valueSMSCode = $.trim($('#sms-code-input').val());
	}).on('blur', function () {
		// 统计
		sa.track('h5_input', {
			'tgt_type': 'text',
			'tgt_name': '还还-注册页-短信验证码',
			'tgt_event_id': 'hh_register_smgCode',
			'url': location.href,
			'position': ''
		});
	});
	$('#password-input').on('focus', function () {
		currValid > 4 && errorHide();
		currValid = 0;
		inputLocate();
	}).on('input blur', function () {
		if (!validPassword(true)) {
			return;
		}
		valuePassword = $.trim($('#password-input').val());
	}).on('blur', function () {
		// 统计
		sa.track('h5_input', {
			'tgt_type': 'text',
			'tgt_name': '还还-注册页-设置密码',
			'tgt_event_id': 'hh_register_loginPassword',
			'url': location.href,
			'position': ''
		});
	});
	$('#phone-clear').on('touchstart mousedown', function () {
		isNewUserAjaxAbort = true;
	}).on('click', function () {
		isNewUserAjaxAbort = true;
		$('#phone-input').val('');
		$('#phone-clear').hide();
		valuePhone = '';
		valueImgCode = '';
		valueSMSCode = '';
		valuePassword = '';
	});
	$('#image-code-button').on('click', function () {
		// 统计
		sa.track('h5_clk', {
			'tgt_type': 'icon',
			'tgt_name': '还还-注册页-刷新图形验证码',
			'tgt_event_id': 'hh_register_refresh_graphicCode',
			'target_url': '',
			'url': location.href
		});
		$('#image-code-picture').attr('src', 'https://ac.ppdai.com/ValidateCode/Image?tmp=' + Math.round(Math.random() * Math.pow(10, 10)));
		ignoreValidImgCode = 0;
	});
	$('#sms-code-button').on('click', function () {
		// 统计
		sa.track('h5_clk', {
			'tgt_type': 'button',
			'tgt_name': '还还-注册页-获取短信验证码',
			'tgt_event_id': 'hh_register_resend_msgCode',
			'target_url': '',
			'url': location.href
		});
		$('#image-code-input').trigger('blur', true);
		if ($(this).parent().hasClass('sending')) {
			return;
		}
		if (!validPhone()) {
			return;
		}
		valueImgCode = valueImgCode || $.trim($('#image-code-input').val());
		if (!validImgCode()) {
			return;
		}
		cookieResultArr = /(?:^| )hkcr_sms_ts=([^;]*)(?:; |$)/.exec(document.cookie);
		localTimestamp = new Date().getTime();
		if (cookieResultArr && localTimestamp - decodeURIComponent(cookieResultArr[1]) < 60 * 1000) {
			errorShow('短信验证码请求频繁，请稍后再试');
			return;
		}
		checkImgCode().done(function (res) {
			if (res.Code != 1) {
				errorShow('图形验证码输入错误，请重新输入');
				return;
			}
			$('#sms-code-button').html('重发验证码').parent().addClass('sending');
			sendTimeout = setTimeout(function () {
				setResendStatus(59);
			}, 1000);
			sendSMSCode().done(function (res) {
				if (res.Code != 1) {
					errorShow('短信验证码发送失败');
					return;
				}
				localTimestamp = new Date().getTime();
				document.cookie = 'hkcr_sms_ts=' + localTimestamp + '; expires=' + new Date(localTimestamp + 5 * 60 * 1000) + ';';
				ignoreValidImgCode = 1;
			});
		});
	});
	$('#user-service-protocol').on('click', function () {
		// 统计
		sa.track('h5_clk', {
			'tgt_type': 'text',
			'tgt_name': '还还-注册页-用户服务协议',
			'tgt_event_id': 'hh_register_srv_item',
			'target_url': '',
			'url': location.href
		});
	});
	$('#borrower-protocol').on('click', function () {
		// 统计
		sa.track('h5_clk', {
			'tgt_type': 'text',
			'tgt_name': '还还-注册页-借入者协议',
			'tgt_event_id': 'hh_register_loan_agreement',
			'target_url': '',
			'url': location.href
		});
	});
	$('#to-top-button').on('click', function () {
		$('.register').children('section').first().get(0).scrollIntoView();
	});
	$('#register-form-submit').on('click', function () {
		// 统计
		sa.track('h5_clk', {
			'tgt_type': 'button',
			'tgt_name': '还还-注册页-领50000额度',
			'tgt_event_id': 'hh_register_amount',
			'target_url': '',
			'url': location.href
		});
		if (!validPhone()) {
			return;
		}
		if (!validImgCode()) {
			return;
		}
		if (!validSMSCode()) {
			return;
		}
		if (!validPassword()) {
			return;
		}
		checkSMSCode().done(function (res) {
			if (res.Code != 1) {
				errorShow('短信验证码输入错误，请重新输入');
				return;
			}
			$.post('//ac.ppdai.com/ActivityPage/Index', {
				'ActivityID': '85',
				'Mobile': valuePhone,
				'Password': valuePassword,
				'SmsValidateCode': valueSMSCode,
				'UserRole': '8',
				'Redirect': '',
				'WeixinCode': '',
				'WeixinFailRedirect': 'https://m.huankachaoren.com/register'
			}).done(function (res) {
				if (res.Code != 1) {
					errorShow(res.Message);
					return;
				}
				$.ajax({
					'type': "get",
					'dataType': "jsonp",
					'url': "//m.ppdai.com/Activity/ThirdInviteNewUserRecordSuccess",
					'data': {
						'mobile': $.trim($('#phone-input').val()),
						'id': newId,
						'source': source
					}
				}).always(function () {
					showNewUserAlert();
				});
			});
		});
	});
	$('#app-download').on('click', function () {
		if (registeredOrNew === 0) {
			// 统计
			sa.track('h5_clk', {
				'tgt_type': 'button',
				'tgt_name': '还还-老用户弹框-app下载',
				'tgt_event_id': 'hh_register_oldCustomers_appDownload',
				'target_url': '',
				'url': location.href
			});
		}
		if (registeredOrNew === 1) {
			// 统计
			sa.track('h5_clk', {
				'tgt_type': 'button',
				'tgt_name': '还还-新用户弹框-app下载',
				'tgt_event_id': 'hh_register_newCustomers_appDownload',
				'target_url': '',
				'url': location.href
			});
		}
		try {
			if (source in jrttObj) {
				_taq.push(jrttObj[source]);
			}
		} catch (ex) {
			console.warn('检查头条文件引入');
		}
		var client = uaClient();
		if (client === 'ios') {
			top.location.href = GetQueryString("url") || 'https://a.app.qq.com/o/simple.jsp?pkgname=com.ppdai.huanapp';
		} else if (client === 'wechat') {
			top.location.href = 'https://m.ppdai.com/act/huan/down.html?source=' + source;
		} else {
			top.location.href = 'https://m.ppdai.com/down/huanhuan/' + source;
		}
	});
	$('#error-alert').on('click', function () {
		errorHide();
	});
});
